<template>
  <div class="login-container">
    <div class="login">

      <div class="login-form">
        <div class="logo">
          <img src="../../assets/common/logo1.png" alt="">
        </div>
        <el-card shadow="never" class="login-card">
          <div slot="header" class="login-tab">
            <div class="login-tab-title" style="color: rgb(25, 171, 141); border-bottom-color: rgb(25, 171, 141);">
              手机号码登录
            </div>
            <div class="login-tab-title">扫描登录</div>
          </div>
          <!--登录表单-->
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left">

            <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
              <el-input
                ref="username"
                v-model="loginForm.mobile"
                placeholder="请输入手机号码"
                name="mobile"
                type="text"
                tabindex="1"

              />
            </el-form-item>

            <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
            </el-form-item>
            <el-form-item style="border:none" prop="isAgree">
              <el-checkbox v-model="loginForm.isAgree">
                用户平台使用协议
              </el-checkbox>
            </el-form-item>
<!-- prevent:阻止元素的默认行为-->
            <el-button :loading="loading" type="success"
                       style="width:100%;margin-bottom:20px;background-color: rgb(25, 171, 141);"
                       @click.native.prevent="handleLogin">登录
            </el-button>
            <div class="tips">
              <span style="margin-right:20px;">仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息 (如手机号、身份证号等)!</span>
            </div>

          </el-form>
        </el-card>

      </div>
      <div class="login-logo"></div>
    </div>


  </div>
</template>

<script>


export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        isAgree: process.env.NODE_ENV === 'development'
      },
      loginRules: {
        mobile: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'

        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }, {
          min: 6,
          max: 16,
          message: '密码长度应该为6-16位之间',
          trigger: 'blur'

        }],
        // required只能检测 null undefined ""
        isAgree: [{
          validator: (rule, value, callback) => {
            // rule校验规则
            // value 校验的值
            // callback 函数 - promise resolve reject
            // callback() callback(new Error(错误信息))
            value ? callback() : callback(new Error('您必须勾选用户的使用协议'))
          }
        }]
      },
      loading: false,
      passwordType: 'password',
      // redirect: undefined
    }
  },
  methods: {
    // 显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    //点击登录
    handleLogin() {
      this.$refs.loginForm.validate(async( vaild)=>{
        console.log(vaild)
        if(vaild){
          console.log(this.loginForm)
          await this.$store.dispatch('user/login',this.loginForm)
          // 跳转主页
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: #ccc;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    //background: rgba(0, 0, 0, 0.1);
    border: 1px solid #ccc;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>


.login-container {
  display: flex;
  min-height: 100%;
  width: 100%;
  //background-color: ;
  overflow: hidden;

  .login {
    width: 1200px;
    height: 700px;
    //background-color: pink;
    //border:1px solid #ccc;
    border-radius: 22px;
    margin: auto;
    display: flex;
    background-color: #FAFBFC;
    //阴影
    box-shadow: 0 -3em 3em rgba(0, 0, 0, 0.1),
    0 0 0 2px rgb(255, 255, 255),
    0.3em 0.3em 1em rgba(0, 0, 0, 0.3);;

    .login-logo {
      width: 60%;
      background: url("../../assets/common/login_back.png") no-repeat center / cover;
      overflow: hidden;

    }

    .login-form {
      position: relative;
      width: 35%;
      //max-width: 100%;
      padding: 95px 15px 0;
      margin: 0 auto;
      //overflow: hidden;
      .logo {
        position: absolute;
        top: 10px;
        left: -30px;

        //img {
        //  width: 200px;
        //  height: 90px;
        //}
      }

      .login-card {
        //box-shadow: 0px 2px 18px 0px rgba(12, 29, 42, 0.1);
        width: 100%;
        min-height: 500px;
        box-shadow: 0px 2px 18px 0px rgba(12, 29, 42, 0.1);
        background: #fff;
        //padding: 0px 32px 24px 32px;
        position: relative;
        flex-shrink: 0;
        border-radius: 4px;

        .login-tab {
          //height: 80px;
          display: flex;
          text-align: center;
          justify-content: space-around;
          padding: 0 0;

        }

        .login-tab-title {
          //width: 50px;
          cursor: pointer;
          margin-top: 10px;
        }
      }

      el-input {
        width: 350px;
        height: 44px;
        color: #fff;

        .el-input__inner {
          background: #f4f5fb;
        }
      }

      .el-checkbox {
        color: #606266;
      }

      .tips {
        font-size: 12px;
        color: red;
        margin-bottom: 5px;

        span {
          &:first-of-type {
            margin-right: 16px;
          }
        }
      }

      .svg-container {
        padding: 6px 5px 6px 15px;
        color: #ccc;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
      }
    }
  }

}
</style>
